package com.heima.user.service.impl;

import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.dtos.UserRelationDTO;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.service.ApUserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    /**
     * 关注取关
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDTO dto) {
        //校验参数
        if (dto.getAuthorApUserId() == null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE,"作者对应的uid不存在");
        }
        Short operation = dto.getOperation();
        if (operation == null || operation.intValue() != 0 && operation.intValue()!=1){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE,"关注类型错误");
        }
        //校验用户登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer id = user.getId();
        Integer authorApUserId = dto.getAuthorApUserId();

        //判断不可以关注自己
        if (id.equals(authorApUserId)){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"不可以关注自己");
        }

        //检验之前是否关注过
        Double score = stringRedisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + id, String.valueOf(authorApUserId));
        if (operation.intValue() == 0 && score != null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"您已经关注,不需要再次关注");
        }

        try {
            //判断operation 是 1  还是  0
            if (operation.intValue() == 0){
                //关注
                //                                      关注                     我的id               作者                       时间
                stringRedisTemplate.opsForZSet().add(UserRelationConstants.FOLLOW_LIST+id,String.valueOf(authorApUserId),System.currentTimeMillis());
                //                                       粉丝
                stringRedisTemplate.opsForZSet().add(UserRelationConstants.FANS_LIST+authorApUserId,String.valueOf(id),System.currentTimeMillis());

            }else {
                stringRedisTemplate.opsForZSet().remove(UserRelationConstants.FOLLOW_LIST+id,String.valueOf(authorApUserId));
                //                                       粉丝
                stringRedisTemplate.opsForZSet().remove(UserRelationConstants.FANS_LIST+authorApUserId,String.valueOf(id));
            }
        } catch (Exception e) {
            e.printStackTrace();
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR);
        }

        return ResponseResult.okResult();
    }
}
